package com.itstyle.seckill.topic.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Auther: liuwenxiang
 * @Date: 2020/4/10 00:37
 * @Description:
 */
public class ExecuteAndQueue {


    private static  ExecutorService executorService = Executors.newCachedThreadPool();

    private  static LinkedBlockingQueue<String> queue=new LinkedBlockingQueue<String>();
    private ExecuteAndQueue(){

    }

    public static class Single{
       private static ExecuteAndQueue executeAndQueue;
        {
            executeAndQueue =new ExecuteAndQueue();
        }
       public static ExecuteAndQueue getInstance(){
            return executeAndQueue;
        }
    }

    public static ExecuteAndQueue getInstance(){
        return Single.getInstance();
    }


    public static class Thread implements Runnable{


        @Override
        public void run() {
            try {
                System.out.println("消费："+queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        for(int i=0;i<10;i++){
            try {
                queue.put(i+"");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("---"+queue.size());
        executorService.execute(new Thread());
        for(int i=10;i<20;i++){
            try {
                queue.put(i+"");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("++++"+queue.size());
    }


}
